#include "StdAfx.h"
#include "StackAllocatorManager.h"


template<> StackAllocatorManager* Ogre::Singleton<StackAllocatorManager>::msSingleton = 0;
StackAllocatorManager* StackAllocatorManager::getSingletonPtr(void)
{
    return msSingleton;
}
StackAllocatorManager& StackAllocatorManager::getSingleton(void)
{  
    assert( msSingleton );  return ( *msSingleton );  
}


StackAllocatorManager::StackAllocatorManager(void)
{

	allocatorBottom = malloc(ALLOCATOR_SIZE);
	allocatorPointer = allocatorBottom;
	int allocatedMemory = 0;
}


StackAllocatorManager::~StackAllocatorManager(void)
{
	delete allocatorBottom;
}


IStackAllocator* StackAllocatorManager::getLevelScopeAllocator(Ogre::String allocatorName)
{
	if(levelScopeAllocatorMap.find(allocatorName)!= levelScopeAllocatorMap.end())
	{
		return levelScopeAllocatorMap[allocatorName];
	}
	else
	{
		return 0;
	}
}


void StackAllocatorManager::addLevelScopeAllocator(Ogre::String allocatorName, IStackAllocator* allocator)
{
	levelScopeAllocatorMap[allocatorName] = allocator;

}

int StackAllocatorManager::getFreeMemory()
{
	int freeMemory = 0;

	for(std::map<Ogre::String,IStackAllocator*>::iterator i= levelScopeAllocatorMap.begin(); i!=levelScopeAllocatorMap.end(); i++)
	{
		freeMemory+= (*i).second->getFreeObjectCount()* (*i).second->getSizeOfObject();
	}

	return freeMemory;
}

int StackAllocatorManager::getAllMemory()
{

	int allMemory = 0;

	for(std::map<Ogre::String,IStackAllocator*>::iterator i= levelScopeAllocatorMap.begin(); i!=levelScopeAllocatorMap.end(); i++)
	{
		allMemory+= (*i).second->getMaxObjectCount()* (*i).second->getSizeOfObject();
	}

	return allMemory;
}

void StackAllocatorManager::resetLevelScopeAllocators()
{

	for(std::map<Ogre::String,IStackAllocator*>::iterator i= levelScopeAllocatorMap.begin(); i!=levelScopeAllocatorMap.end(); i++)
	{
		(*i).second->resetAllocator();
	}
}